package com.ning.action;

import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.ning.dao.ApplyDao;
import com.ning.pojo.Apply;
import com.ning.pojo.User;
import com.opensymphony.xwork2.ActionSupport;

public class ApplyAction extends ActionSupport {
	
	/**
	 * 查询当前员工的所有申请
	 * @return
	 * @throws Exception
	 */
	public String findApplyByUser() throws Exception{
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
		list = applyDao.findApplyByUser(user.getId());
		return SUCCESS;
	}
	
	public String findApplyByCompetent() throws Exception{
		User user = (User)ServletActionContext.getRequest().getSession().getAttribute("user");
		list = applyDao.findApplyByCompetent(user.getDept().getId());
		return "competentSuccess";
	}
	
	public String findApplyByManager() throws Exception{
		list = applyDao.findApplyByManager();
		return "managerSuccess";
	}
	
	public String findApplyByBoss() throws Exception{
		list = applyDao.findApplyByBoss();
		return "bossSuccess";
	}
	
	
	/**
	 * 提交申请
	 * @return
	 * @throws Exception
	 */
	public String submitApply() throws Exception{
		Date date = new Date();
		apply.setStartTime(date);
		apply.setState("0");
		apply.setFlag("0");
		apply.setUser((User)ServletActionContext.getRequest().getSession().getAttribute("user"));
		applyDao.save(apply);
		return "submit";
	}
	
	/**
	 * 部门主管审批
	 * @return
	 * @throws Exception
	 */
	public String competentSubmit() throws Exception{
		Apply apply_ = applyDao.getApplyById(applyId);
		if(1== arg){
			apply_.setFlag("1");
			applyDao.update(apply_);
		}else if(0 == arg){
			apply_.setState("2");
			apply_.setFlag("3");
			applyDao.update(apply_);
		}
		return "competentSubmit";
	}
	
	/**
	 * 经理审批
	 * @return
	 * @throws Exception
	 */
	public String managerSubmit() throws Exception{
		Apply apply_ = applyDao.getApplyById(applyId);
		if(1== arg){
			if(1500<=apply_.getSalary()){
				apply_.setFlag("2");
				applyDao.update(apply_);
			}else{
				apply_.setState("1");
				apply_.setFlag("3");
				applyDao.update(apply_);
			}
		}else if(0 == arg){
			apply_.setState("2");
			apply_.setFlag("3");
			applyDao.update(apply_);
		}
		return "managerSubmit";
	}
	
	/**
	 * 老板审批
	 * @return
	 * @throws Exception
	 */
	public String bossSubmit() throws Exception{
		Apply apply_ = applyDao.getApplyById(applyId);
		if(1== arg){
			apply_.setState("1");
			apply_.setFlag("3");
			applyDao.update(apply_);
		}else if(0 == arg){
			apply_.setState("2");
			apply_.setFlag("3");
			applyDao.update(apply_);
		}
		return "bossSubmit";
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	private ApplyDao applyDao;
	private List<Apply> list;
	private Apply apply;
	private Integer arg;
	private Integer applyId;
	public void setApplyDao(ApplyDao applyDao) {
		this.applyDao = applyDao;
	}
	public List<Apply> getList() {
		return list;
	}
	public void setList(List<Apply> list) {
		this.list = list;
	}
	public Apply getApply() {
		return apply;
	}
	public void setApply(Apply apply) {
		this.apply = apply;
	}
	public Integer getArg() {
		return arg;
	}
	public void setArg(Integer arg) {
		this.arg = arg;
	}
	public Integer getApplyId() {
		return applyId;
	}
	public void setApplyId(Integer applyId) {
		this.applyId = applyId;
	}
}
